<script setup lang="ts">
import cpNavBar from '@/components/cp-nav-bar.vue'
import cpIcons from '@/components/cp-icons.vue'
import { mobileRules, passwordRules } from '@/utils/rules'
import type { User } from '@/types/user'
import { getLogin } from '@/api/user'
import { useUserStore } from '@/stores'
import { useRouter } from 'vue-router'
import { showToast, showSuccessToast } from 'vant'
import { ref } from 'vue'
// 表单数据
const mobile = ref('13230000001')
const password = ref('abc12345')
// 控制密码是否显示
const show = ref(false)
const agree = ref(false)
const sotre = useUserStore()
const router = useRouter()
const login = async () => {
  if (!agree.value) return showToast('请勾选用户协议')
  try {
    const data = await getLogin<User>({
      mobile: mobile.value,
      password: password.value
    })
    sotre.setUser(data.data)
    showSuccessToast('登录成功！')
    router.push('/')
  } catch (err: any) {
    showToast(err.message || '网络波动异常')
  }
}
</script>

<template>
  <div class="login-page">
    <cpNavBar right-text="注册" @click-right="$router.push('/register')"></cpNavBar>
    <div class="login-head">
      <h3>密码登录</h3>
      <a href="javascript:;">
        <span>短信验证码登录</span>
        <van-icon name="arrow"></van-icon>
      </a>
    </div>
    <!-- form 表单 -->
    <van-form autocomplete="off" @submit="login">
      <van-field placeholder="请输入手机号" type="text" v-model="mobile" :rules="mobileRules"> </van-field>
      <van-field
        placeholder="请输入密码"
        v-model="password"
        :type="show ? 'text' : 'password'"
        class="pass"
        :rules="passwordRules"
      >
        <template #button>
          <cpIcons @click="show = !show" :name="`login-eye-${show ? 'on' : 'off'}`"></cpIcons>
        </template>
      </van-field>
      <div class="cp-cell">
        <van-checkbox v-model="agree">
          <span>我已同意</span>
          <a href="javascript:;">用户协议</a>
          <span>及</span>
          <a href="javascript:;">隐私条款</a>
        </van-checkbox>
      </div>
      <div class="cp-cell">
        <van-button native-type="submit" block round type="primary">登 录 </van-button>
      </div>
      <div class="cp-cell">
        <a href="javascript:;">忘记密码？</a>
      </div>
    </van-form>
    <div class="login-other">
      <van-divider>第三方登录</van-divider>
      <div class="icon">
        <img src="@/assets/qq.svg" alt="" />
      </div>
    </div>
  </div>
</template>
<style scoped lang="scss">
::v-deep() {
  .van-nav-bar {
    &__arrow {
      font-size: 18px;
      color: var(--cp-text1);
    }
    &__text {
      font-size: 15px;
    }
  }
}
.login {
  &-page {
    padding-top: 46px;
  }
  &-head {
    padding: 30px 30px 50px;
    display: flex;
    justify-content: space-between;
    align-items: flex-end;
    line-height: 1;
    h3 {
      font-weight: normal;
      font-size: 24px;
    }
    a {
      font-size: 15px;
    }
  }
  &-other {
    margin-top: 60px;
    padding: 0 30px;
    .icon {
      display: flex;
      justify-content: center;
      img {
        width: 36px;
        height: 36px;
        padding: 4px;
      }
    }
  }
}
.van-form {
  padding: 0 14px;
  .cp-cell {
    height: 52px;
    line-height: 24px;
    padding: 14px 16px;
    box-sizing: border-box;
    display: flex;
    align-items: center;
    .van-checkbox {
      a {
        color: var(--cp-primary);
        padding: 0 5px;
      }
    }
  }
}
</style>
